package com.xhdx.ati.adau.controller.service.flow;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.TaskService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xhdx.ati.adau.entity.HolidayEntity;
import com.xhdx.ati.adau.entity.UserEntity;

@Service
public class WorkFlowService {
	private static final Log logger = LogFactory.getLog(WorkFlowService.class);
//	@Autowired
//	private RuntimeService runtimeService;

	@Autowired
	private TaskService taskService;

//	@Autowired
//	private RepositoryService repositoryService;

	/** 一：使用任务ID，查找规则ID，从而获取规则信息 */
	public Object findSysEntityByTaskId(String taskId) {
		Object[] result = null;
		try {
			result = (Object[]) taskService.getVariable(taskId, "result");
		} catch (Exception e) {
			return null;
		}
		return result[0];
	}

	/**
	 *流程变量
	      在流程执行或者任务执行的过程中，用于设置和获取变量，使用流程变量在流程传递的过程中传递业务参数。
	      对应的表：
	      act_ru_variable：正在执行的流程变量
	      act_hi_varinst：流程变量历史表
	   @description 设置流程变量
	 * @param taskId
	 * @param user
	 * @param holidayEntity
	 */
	public void setVariables(String taskId, UserEntity user, HolidayEntity holidayEntity) {
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("申请人", user.getName());// 在启动时设置流程变量

		// 与任务ID绑定
		taskService.setVariableLocal(taskId, "请假天数", holidayEntity.getDay());
		taskService.setVariable(taskId, "请假日期", holidayEntity.getDate());
		taskService.setVariable(taskId, "请假原因", holidayEntity.getRemark());

		taskService.setVariable(taskId, "用户信息", user);
		logger.info("申请人信息");
	}
	/**
	 *@description 获取流程变量
	 * @param taskId
	 * @return
	 */
	public Map<String, Object> getVariables(String taskId) {
		Map<String, Object> variables = new HashMap<String, Object>();
		Float day = (Float) taskService.getVariable(taskId, "请假天数");
		String date = (String) taskService.getVariable(taskId, "请假日期");
		String remark = (String) taskService.getVariable(taskId, "请假原因");
		HolidayEntity holidayEntity = new HolidayEntity();
		holidayEntity.setDate(date);
		holidayEntity.setRemark(remark);
		holidayEntity.setDay(day);
		
		variables.put("请假信息", holidayEntity);
		
		UserEntity u = (UserEntity) taskService.getVariable(taskId, "用户信息");
		variables.put("用户信息", u);
		return variables;
	}
}
